#include "bsp_temp_algorithm.h"


volatile bsp_ntc ntc={0};

volatile bsp_count count={0};
volatile bsp_modle work={.r=OFF,.bk2=OFF,.modle=TEMP_MODLE_A2S,.offset=0,.bit=0};

volatile float temp_magnification=HARDWARE_B;//计算倍数
volatile bsp_temp_data temp_data={0,0,0,TEMP_PREPARE_ALARM,TEMP_ALARM};
volatile bsp_temp_flag temp_flag={0};

/*低温检测*/
volatile uint16_t temp_low_reset_count=0;
volatile uint8_t low_temp_count=0;
volatile bsp_low_modle low_temp={0};

/*补偿值*/
volatile float increment_2=(TEMP_1_ALARM_SOURCE_MAX-TEMP_1_ALARM_SOURCE_MIN)/160;
volatile float increment_1=(TEMP_1_ALARM_SOURCE_MIN-TEMP_START)/290;
volatile float increment_3=(TEMP_END-TEMP_1_ALARM_SOURCE_MAX)/50;
volatile float temp_alarm_source_min=TEMP_1_ALARM_SOURCE_MIN;
volatile float temp_alarm_source_max=TEMP_1_ALARM_SOURCE_MAX;
volatile unsigned char temp_speed_modle=TEMP_SPEED_MODLE_1;

//const char modle_table_1[8][2]={"A1","A2"," B"," C"," D"," E"," F"," G"};
//const char modle_table_2[3]={' ','S','R'};
//const char modle_table_3[19]={'H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};


/*硬件设计峰值温度4095时，大概温度在235℃*/
/*PT100温度查表 -49~309*/
#ifdef PT100 
#define TABLE_COUNT 559
const float PT100_TEMP_TABLE[TABLE_COUNT]=
{
 /*-49~ -30*/
 80.70,81.10,81.50,81.89,82.29,82.69,83.08,83.48,83.87,84.27,84.67,85.06,85.46,85.85,86.25,86.64,87.04,87.43,87.83,88.22,
 /*-29~ -10*/
 88.62,89.01,89.40,89.80,90.19,90.59,90.98,91.37,91.77,92.16,92.55,92.95,93.34,93.73,94.12,94.52,94.91,95.30,95.69,96.09,
 /*-9~ 0*/
 96.48,96.87,97.26,97.65,98.04,98.44,98.83,99.22,99.61,
 /*0~19*/
 100.00,100.39,100.78,101.17,101.56,101.95,102.34,102.73,103.12,103.51,103.90,104.29,104.68,105.07,105.46,105.85,106.24,106.63,107.02,107.40,
 /*20~39*/
 107.79,108.18,108.57,108.96,109.35,109.73,110.12,110.51,110.90,111.29,111.67,112.06,112.45,112.83,113.22,113.61,114.00,114.38,114.77,115.15,
 /*40~59*/
 115.54,115.93,116.31,116.70,117.08,117.47,117.86,118.24,118.63,119.01,119.40,119.78,120.17,120.55,120.94,121.32,121.71,122.09,122.47,122.86,
 /*60~79*/
 123.24,123.63,124.01,124.39,124.78,125.16,125.54,125.93,126.31,126.69,127.08,127.46,127.84,128.22,128.61,128.99,129.37,129.75,130.13,130.52,
 /*80~99*/
 130.90,131.28,131.66,132.04,132.42,132.80,133.18,133.57,133.95,134.33,134.71,135.09,135.47,135.85,136.23,136.61,136.99,137.37,137.75,138.13,

 /*100~119*/
 138.51,138.88,139.26,139.64,140.02,140.40,140.78,141.16,141.54,141.91,142.29,142.67,143.05,143.43,143.80,144.18,144.56,144.94,145.31,145.69,
 /*120~139*/
 146.07,146.44,146.82,147.20,147.57,147.95,148.33,148.70,149.08,149.46,149.83,150.21,150.58,150.96,151.33,151.71,152.08,152.46,152.83,153.21,
 /*140~159*/
 153.58,153.96,154.33,154.71,155.08,155.46,155.83,156.20,156.58,156.95,157.33,157.70,158.07,158.45,158.82,159.19,159.56,159.94,160.31,160.68,
 /*160~179*/
 161.05,161.43,161.80,162.17,162.54,162.91,163.29,163.66,164.03,164.40,164.77,165.14,165.51,165.89,166.26,166.63,167.00,167.37,167.74,168.11,
 /*180~199*/
 168.48,168.85,169.22,169.59,169.96,170.33,170.70,171.07,171.43,171.80,172.17,172.54,172.91,173.28,173.65,174.02,174.38,174.75,175.12,175.49,
 
 /*200~219*/
 175.86,176.22,176.59,176.96,177.33,177.69,178.06,178.43,178.79,179.16,179.53,179.89,180.26,180.63,180.99,181.36,181.72,182.09,182.46,182.82,
 /*220~239*/
 183.19,183.55,183.92,184.28,184.65,185.01,185.38,185.74,186.11,186.47,186.84,187.20,187.56,187.93,188.29,188.66,189.02,189.38,189.75,190.11,
 /*240~259*/
 190.47,190.84,191.20,191.56,191.92,192.29,192.65,193.01,193.37,193.74,194.10,194.46,194.82,195.18,195.55,195.91,196.27,196.63,196.99,197.35,
 /*260~279*/
 197.71,198.07,198.43,198.79,199.15,199.51,199.87,200.23,200.59,200.95,201.31,201.67,202.03,202.39,202.75,203.11,203.47,203.83,204.19,204.55,
 /*280~299*/
 204.90,205.26,205.62,205.98,206.34,206.70,207.05,207.41,207.77,208.13,208.48,208.84,209.20,209.56,209.91,210.27,210.63,210.98,211.34,211.70,
 
 /*300~319*/
 212.05,212.41,212.76,213.12,213.48,213.83,214.19,214.54,214.90,215.25,215.61,215.96,216.32,216.67,217.03,217.38,217.74,218.09,218.44,218.80,
 /*320~339*/
 219.15,219.51,219.86,220.21,220.57,220.92,221.27,221.63,221.98,222.33,228.68,223.04,223.39,223.74,224.09,224.45,224.80,225.15,225.50,225.85,
 /*340~359*/
 226.21,226.56,226.91,227.26,227.61,227.96,228.31,228.66,229.02,229.37,229.72,230.07,230.42,230.77,231.12,231.47,231.82,232.17,232.52,232.87,
 /*360~379*/
 233.21,233.56,233.91,234.26,234.61,234.96,235.31,235.66,236.00,236.35,236.70,237.05,237.40,237.74,238.09,238.44,238.79,239.13,239.48,239.83,
 /*380~399*/
 240.18,240.52,240.87,241.22,241.56,241.91,242.26,242.60,242.95,243.29,243.64,243.99,244.33,244.68,245.02,245.37,245.71,246.06,246.40,246.75,
 
 /*400~419*/
 247.09,247.44,247.78,248.13,248.47,248.81,249.16,249.50,249.85,250.19,250.53,250.88,251.22,251.56,251.91,252.25,252.59,252.93,253.28,253.62,
 /*420~439*/
 253.96,254.30,254.65,254.99,255.33,255.67,256.01,256.35,256.70,257.04,257.38,257.72,258.06,258.04,258.74,259.08,259.42,259.76,260.10,260.44,
 /*440~459*/
 260.78,261.12,261.46,261.80,262.14,262.48,262.82,263.16,263.50,263.84,264.18,264.52,264.86,265.20,265.53,265.87,266.21,266.55,266.89,267.22,
 /*460~479*/
 267.56,267.90,268.24,268.57,268.91,269.25,269.59,269.92,270.26,270.60,270.93,271.27,271.61,271.94,272.28,272.61,272.95,273.29,273.62,273.96,
 /*480~499*/
 274.29,274.63,274.96,275.30,275.63,275.97,276.30,276.64,276.97,277.31,277.64,277.98,278.31,278.64,278.98,279.31,279.64,279.98,280.31,280.64,
 /*500~509*/
 280.98,281.31,281.64,281.98,282.31,282.64,282.97,283.31,283.64,283.97
};
#elif defined(PT1000)
#define TABLE_COUNT 542
const float PT100_TEMP_TABLE[TABLE_COUNT]=
{
//	/*-49 ~ -40*/
//	807.05, 811.02, 814.98, 818.95, 822.91, 826.88, 830.84, 834.80, 838.76,	842.72,
	/*-40*/
	842.72,
	/*-39 ~ -30*/
	846.67, 850.63, 854.58, 858.54, 862.49, 866.44, 870.39, 874.33, 878.28, 882.22,
	/*-29 ~ -20*/
	886.17, 890.11, 894.05, 897.99, 901.93, 905.87, 909.80, 913.74, 917.67, 921.60,
	/*-19 ~ -10*/
	925.54, 929.47, 933.39, 937.32, 941.25, 945.17, 949.10, 953.02, 956.94, 960.86,
	/*-9~ 0*/
	964.78, 968.70, 972.62, 976.53, 980.45, 984.36, 988.27, 992.18, 996.09, 1000.00,
	
	/*0 ~ 9*/
	1000.00, 1003.91, 1007.81, 1011.72, 1015.62, 1019.53, 1023.43, 1027.33, 1031.23, 1035.13,
  /*10 ~ 19*/
	1039.02, 1042.92, 1046.81, 1050.71, 1054.60, 1058.49, 1062.38, 1066.27, 1070.16, 1074.04,
  /*20 ~ 29*/
	1077.93, 1081.81, 1085.70, 1089.58, 1093.46, 1097.34, 1101.22, 1105.10, 1108.97, 1112.85,
  /*30 ~ 39*/
	1116.72, 1120.59, 1124.47, 1128.34, 1132.21, 1136.07, 1139.94, 1143.81, 1147.67, 1151.53,
  /*40 ~ 49*/
	1155.40, 1159.26, 1163.12, 1166.98, 1170.84, 1174.69, 1178.55, 1182.40, 1186.25, 1190.11,
  /*50 ~ 59*/
	1193.96, 1197.81, 1201.66, 1205.50, 1209.35, 1213.19, 1217.04, 1220.88, 1224.72, 1228.56,
	/*60 ~ 69*/
	1232.40, 1236.24, 1240.08, 1243.91, 1247.75, 1251.58, 1255.41, 1259.25, 1263.08, 1266.90,
  /*70 ~ 79*/
	1270.73, 1274.56, 1278.38, 1282.21, 1286.03, 1289.85, 1293.67, 1297.49, 1301.31, 1305.13,
  /*80 ~ 89*/
	1308.95, 1312.76, 1316.58, 1320.39, 1324.20, 1328.01, 1331.82, 1335.63, 1339.43, 1343.24,
  /*90 ~ 99*/
	1347.04, 1350.85, 1354.65, 1358.45, 1362.25, 1366.05, 1369.85, 1373.65, 1377.44, 1381.23,
  
	/*100 ~ 109*/
	1385.03, 1388.82, 1392.61, 1396.40, 1400.19, 1403.98, 1407.76, 1411.55, 1415.33, 1419.11,
  /*110 ~ 119*/
	1422.90, 1426.68, 1430.46, 1434.23, 1438.01, 1441.79, 1445.56, 1449.33, 1453.11, 1456.88,
	/*120 ~ 129*/
	1460.65, 1464.42, 1468.18, 1471.95, 1475.72, 1479.48, 1483.24, 1487.01, 1490.77, 1494.53,
	/*130 ~ 139*/
	1498.28, 1502.04, 1505.80, 1509.55, 1513.31, 1517.06, 1520.81, 1524.56, 1528.31, 1532.06,
	/*140 ~ 149*/
	1535.81, 1539.55, 1543.30, 1547.04, 1550.78, 1554.52, 1558.26, 1562.00, 1565.74, 1569.48,
	/*150 ~ 159*/
	1573.21, 1576.94, 1580.68, 1584.41, 1588.14, 1591.87, 1595.60, 1599.33, 1603.05, 1606.78,
	/*160 ~ 169*/
	1610.50, 1614.22, 1617.94, 1621.67, 1625.38, 1629.10, 1632.82, 1636.54, 1640.25, 1643.96,
	/*170 ~ 179*/
	1647.68, 1651.39, 1655.10, 1658.81, 1662.51, 1666.22, 1669.92, 1673.63, 1677.33, 1681.03,
	/*180 ~ 189*/
	1684.73, 1688.43, 1692.13, 1695.83, 1699.53, 1703.22, 1706.91, 1710.61, 1714.30, 1717.99,
	/*190 ~ 199*/
	1721.68, 1725.37, 1729.05, 1732.74, 1736.42, 1740.11, 1743.79, 1747.47, 1751.15, 1754.83,
  
	/*200 ~ 209*/
	1758.51, 1762.18, 1765.86, 1769.53, 1773.20, 1776.88, 1780.55, 1784.22, 1787.89, 1791.55,
  /*210 ~ 219*/
	1795.22, 1798.88, 1802.55, 1806.21, 1809.87, 1813.53, 1817.19, 1820.85, 1824.51, 1828.16,
  /*220 ~ 229*/
	1831.82, 1835.47, 1839.12, 1842.77, 1846.42, 1850.07, 1853.72, 1857.36, 1861.01, 1864.65,
  /*230 ~ 239*/
	1868.30, 1871.94, 1875.58, 1879.22, 1882.86, 1886.49, 1890.13, 1893.77, 1897.40, 1901.03,
  /*240 ~ 249*/
	1904.66, 1908.29, 1911.92, 1915.55, 1919.18, 1922.80, 1926.43, 1930.05, 1933.67, 1937.29,
  /*250 ~ 259*/
	1940.91, 1944.53, 1948.15, 1951.77, 1955.38, 1959.00, 1962.61, 1966.22, 1969.83, 1973.44,
  /*260 ~ 269*/
	1977.05, 1980.66, 1984.26, 1987.87, 1991.47, 1995.07, 1998.67, 2002.27, 2005.87, 2009.47,
  /*270 ~ 279*/
	2013.07, 2016.66, 2020.26, 2023.85, 2027.44, 2031.03, 2034.62, 2038.21, 2041.80, 2045.39,
  /*280 ~ 289*/
	2048.97, 2052.56, 2056.14, 2059.72, 2063.30, 2066.88, 2070.46, 2074.04, 2077.61, 2081.19,
  /*290 ~ 299*/
	2084.76, 2088.33, 2091.90, 2095.47, 2099.04, 2102.61, 2106.18, 2109.74, 2113.31, 2116.87,
	
  /*300 ~ 309*/
	2120.43, 2123.99, 2127.55, 2131.11, 2134.67, 2138.23, 2141.78, 2145.34, 2148.89, 2152.44,
  /*310 ~ 319*/
	2155.99, 2159.54, 2163.09, 2166.64, 2170.18, 2173.73, 2177.27, 2180.81, 2184.35, 2187.89,
  /*320 ~ 329*/
	2191.43, 2194.97, 2198.51, 2202.04, 2205.58, 2209.11, 2212.64, 2216.17, 2219.70, 2223.23,
  /*330 ~ 339*/
	2226.76, 2230.29, 2233.81, 2237.34, 2240.86, 2244.38, 2247.90, 2251.42, 2254.94, 2258.46,
  /*340 ~ 349*/
	2261.97, 2265.49, 2269.00, 2272.51, 2276.02, 2279.53, 2283.04, 2286.55, 2290.06, 2293.56,
  /*350 ~ 359*/
	2297.07, 2300.57, 2304.07, 2307.57, 2311.07, 2314.57, 2318.07, 2321.56, 2325.06, 2328.55,
  /*360 ~ 369*/
	2332.05, 2335.54, 2339.03, 2342.52, 2346.01, 2349.49, 2352.98, 2356.46, 2359.95, 2363.43,
  /*370 ~ 379*/
	2366.91, 2370.39, 2373.87, 2377.35, 2380.82, 2384.30, 2387.77, 2391.25, 2394.72, 2398.19,
  /*380 ~ 389*/
	2401.66, 2405.13, 2408.60, 2412.06, 2415.53, 2418.99, 2422.45, 2425.92, 2429.38, 2432.84,
  /*390 ~ 399*/
	2436.29, 2439.75, 2443.21, 2446.66, 2450.12, 2453.57, 2457.02, 2460.47, 2463.92, 2467.37,
  
	/*400 ~ 409*/
	2470.81, 2474.26, 2477.70, 2481.14, 2484.59, 2488.03, 2491.47, 2494.91, 2498.34, 2501.78,
  /*410 ~ 419*/
	2505.21, 2508.65, 2512.08, 2515.51, 2518.94, 2522.37, 2525.80, 2529.23, 2532.65, 2536.08,
  /*420 ~ 429*/
	2539.50, 2542.92, 2546.35, 2549.77, 2553.18, 2556.60, 2560.02, 2563.43, 2566.85, 2570.26,
  /*430 ~ 439*/
	2573.67, 2577.08, 2580.49, 2583.90, 2587.31, 2590.72, 2594.12, 2597.52, 2600.93, 2604.33,
  /*440 ~ 449*/
	2607.73, 2611.13, 2614.53, 2617.92, 2621.32, 2624.71, 2628.11, 2631.50, 2634.89, 2638.28,
  /*450 ~ 459*/
	2641.67, 2645.06, 2648.44, 2651.83, 2655.21, 2658.60, 2661.98, 2665.36, 2668.74, 2672.12,
  /*460 ~ 469*/
	2675.49, 2678.87, 2682.25, 2685.62, 2688.99, 2692.36, 2695.73, 2699.10, 2702.47, 2705.84,
  /*470 ~ 479*/
	2709.20, 2712.57, 2715.93, 2719.29, 2722.66, 2726.02, 2729.37, 2732.73, 2736.09, 2739.44,
  /*480 ~ 489*/
	2742.80, 2746.15, 2749.50, 2752.85, 2756.20, 2759.55, 2762.90, 2766.25, 2769.59, 2772.93,
  /*490 ~ 499*/
	2776.28, 2779.62, 2782.96, 2786.30, 2789.64, 2792.97, 2796.31, 2799.64, 2802.98, 2806.31,
	
	/*500*/
	2809.64,
//	/*500 ~ 509*/
//	2809.64, 2812.97, 2816.30, 2819.63, 2822.95, 2826.28, 2829.60, 2832.93, 2836.25, 2839.57,
//	/*510 ~ 519*/
//	2842.89, 2846.21, 2849.52, 2852.84, 2856.15, 2859.47, 2862.78, 2866.09, 2869.40, 2872.71,
//	/*520 ~ 529*/
//	2876.02, 2879.33, 2882.63, 2885.94, 2889.24, 2892.54, 2895.84, 2899.14, 2902.44, 2905.74,
//	/*530 ~ 539*/
//	2909.04, 2912.33, 2915.63, 2918.92, 2922.21, 2925.50, 2928.79, 2932.08, 2935.37, 2938.65,
//	/*540 ~ 549*/
//	2941.94, 2945.22, 2948.50, 2951.78, 2955.07, 2958.34, 2961.62, 2964.90, 2968.17, 2971.45,
//	/*550 ~ 559*/
//	2974.72, 2977.99, 2981.27, 2984.54, 2987.80, 2991.07, 2994.34, 2997.60, 3000.87, 3004.13,
//	/*560 ~ 569*/
//	3007.39, 3010.65, 3013.91, 3017.17, 3020.43, 3023.68, 3026.94, 3030.19, 3033.45, 3036.70,
//	/*570 ~ 579*/
//	3039.95, 3043.20, 3046.44, 3049.69, 3052.94, 3056.18, 3059.42, 3062.67, 3065.91, 3069.15,
//	/*580 ~ 589*/
//	3072.39, 3075.62, 3078.86, 3082.10, 3085.33, 3088.56, 3091.79, 3095.03, 3098.25, 3101.48,
//	/*590 ~ 599*/
//	3104.71, 3107.94, 3111.16, 3114.38, 3117.61, 3120.83, 3124.05, 3127.27, 3130.49, 3133.70,

//	/*600 ~ 609*/
	//3136.92,3140.13, 3143.35, 3146.56, 3149.77, 3152.98, 3156.19, 3159.39, 3162.60, 3165.81,
//	/*610 ~ 619*/
//	3169.01, 3172.21, 3175.42, 3178.62, 3181.82, 3185.01, 3188.21, 3191.41, 3194.60, 3197.80,
//	/*620 ~ 629*/
//	3200.99, 3204.18, 3207.37, 3210.56, 3213.75, 3216.93, 3220.12, 3223.30, 3226.49, 3229.67,
//	/*630 ~ 639*/
//	3232.85, 3236.03, 3239.21, 3242.39, 3245.56, 3248.74, 3251.91, 3255.08, 3258.26, 3261.43,
//	/*640 ~ 649*/
//	3264.60, 3267.76, 3270.93, 3274.10, 3277.26, 3280.42, 3283.59, 3286.75, 3289.91, 3293.07,
//	/*650 ~ 659*/
//	3296.23, 3299.38, 3302.54, 3305.69, 3308.85, 3312.00, 3315.15, 3318.30, 3321.45, 3324.59,
//	/*660 ~ 669*/
//	3327.74, 3330.89, 3334.03, 3337.17, 3340.31, 3343.46, 3346.59, 3349.73, 3352.87, 3356.01,
//	/*670 ~ 679*/
//	3359.14, 3362.27, 3365.41, 3368.54, 3371.67, 3374.80, 3377.92, 3381.05, 3384.18, 3387.30,
//	/*680 ~ 689*/
//	3390.42, 3393.55, 3396.67, 3399.79, 3402.91, 3406.02, 3409.14, 3412.25, 3415.37, 3418.48,
//	/*690 ~ 699*/
//	3421.59, 3424.70, 3427.81, 3430.92, 3434.03, 3437.13, 3440.24, 3443.34, 3446.44, 3449.55,
//	
//	/*700 ~ 709*/
//	3452.65, 3455.74, 3458.84, 3461.94, 3465.03, 3468.13, 3471.22, 3474.31, 3477.41, 3480.49,
//	/*710 ~ 719*/
//	3483.58, 3486.67, 3489.76, 3492.84, 3495.93, 3499.01, 3502.09, 3505.17, 3508.25, 3511.33,
//	/*720 ~ 729*/
//	3514.41, 3517.48, 3520.56, 3523.63, 3526.70, 3529.77, 3532.84, 3535.91, 3538.98, 3542.05,
//	/*730 ~ 739*/
//	3545.11, 3548.18, 3551.24, 3554.30, 3557.36, 3560.42, 3563.48, 3566.54, 3569.59, 3572.65,
//	/*740 ~ 749*/
//	3575.70, 3578.76, 3581.81, 3584.86, 3587.91, 3590.96, 3594.00, 3597.05, 3600.09, 3603.14,
//	/*750 ~ 759*/
//	3606.18, 3609.22, 3612.26, 3615.30, 3618.34, 3621.37, 3624.41, 3627.44, 3630.48, 3633.51,
//	/*760 ~ 769*/
//	3636.54, 3639.57, 3642.60, 3645.62, 3648.65, 3651.68, 3654.70, 3657.72, 3660.74, 3663.76,
//	/*770 ~ 779*/
//	3666.78, 3669.80, 3672.82, 3675.83, 3678.85, 3681.86, 3684.87, 3687.89, 3690.90, 3693.90,
//	/*780 ~ 789*/
//	3696.91, 3699.92, 3702.92, 3705.93, 3708.93, 3711.93, 3714.93, 3717.93, 3720.93, 3723.93,
//	/*790 ~ 799*/
//	3726.93, 3729.92, 3732.91, 3735.91, 3738.90, 3741.89, 3744.88, 3747.87, 3750.85, 3753.84,
//	/*800*/
//	3756.82,
};
#endif 


/*
功能：NTC AD值转温度值
输入：r_ad：温度传感器的AD采样值
输出：转换后的温度值
说明：将NTC采集出的AD值进行算术转化成温度。
*/

float Temp_AD_To_Temp_Source(uint16_t r_ad)
{
	float rt,temp,vt=0;
	
	vt=3.3f*r_ad/4095;
	
	rt=(3.3f - vt)*100000/vt;   //100K
	
	temp=rt/NTC_R;
	
	temp=log(temp);
	
	temp /=NTC_B;
	
	temp +=(1/NTC_T2);
	
	temp = 1/temp;
	
	temp -= NTC_K;

	return temp;
}


/*
功能：参数读取配置
输入：null
输出：null
说明：初上电对EEPROM储存的参数进行读取。
parameter
*/
void bsp_Temp_Parameter_Config(void)
{ 
	/*test start*/
//float temp_h=17.7254;  17.558
//eeprom_write_buff(TEMP_ALGORITHM_CALIBRATION_ADDRESS,(uint8_t*)&temp_h,TEMP_ALGORITHM_CALIBRATION_LEN);
	/*test end*/
	eeprom_read_buff(TEMP_ALGORITHM_CALIBRATION_ADDRESS,(uint8_t*)&temp_magnification,TEMP_ALGORITHM_CALIBRATION_LEN);
	printf("m:%.7f\n",temp_magnification);
	#ifdef EVENT_MODLE
	bsp_event_renew(EVENT_ALARM_CHANGE|EVENT_PREPARE_ALARM_CHANGE);//事件记录
	#endif

	if(15>=temp_magnification||temp_magnification>=20)
	{
		temp_magnification=HARDWARE_B;
	}
}


/*
功能：阈值修改
输入：temp_alarm：修改目标报警阈值
	  temp_prepare_alarm：修改目标预警阈值
	  modle_ht:1:高温非国标模式启动，0：火灾探测器模式
输出：null
说明：
*/
void bsp_Temp_Alarm_Change(uint16_t temp_alarm,uint16_t temp_prepare_alarm,uint8_t modle_ht)
{
		/*报警*/
		/*高温模式*/
		if(modle_ht)
		{
			temp_data.alarm=temp_alarm;
			temp_data.prepare_alarm=temp_prepare_alarm;
		}
		/*火灾探测模式*/
		else
		{
			if(work.modle>2)//非A1模式
			{
				if(temp_alarm!=0)
				{
					if(temp_alarm>=(TEMP_ALARM_MIN+work.offset)&&temp_alarm<=(TEMP_ALARM_MAX+work.offset))
						temp_data.alarm=temp_alarm;
					else if(temp_alarm<(TEMP_ALARM_MIN+work.offset))
						temp_data.alarm=TEMP_ALARM_MIN+work.offset;
					else
						temp_data.alarm=TEMP_ALARM_MAX+work.offset;
				}
				else
				{
					temp_data.alarm=TEMP_ALARM+work.offset;
				}
				/*预警*/
				if(temp_prepare_alarm!=0)
				{
					if(temp_prepare_alarm<temp_alarm)
						temp_data.prepare_alarm=temp_prepare_alarm;
					else
						temp_data.prepare_alarm=temp_data.alarm-10;
				}
				else
				{
					temp_data.prepare_alarm=temp_data.alarm-10;
				}
				
			}
			else//A1模式
			{
				if(temp_alarm!=0)
				{
					if((temp_alarm>=TEMP_ALARM_MIN)&&(temp_alarm<=TEMP_ALARM_MAX-5))
						temp_data.alarm=temp_alarm;
					else if(temp_alarm<TEMP_ALARM_MIN)
						temp_data.alarm=TEMP_ALARM_MIN;
					else
						temp_data.alarm=TEMP_ALARM_MAX-5;
				}
				else
				{
					temp_data.alarm=TEMP_ALARM;
				}
				/*预警*/
				if(temp_prepare_alarm!=0)
				{
					if(temp_prepare_alarm<temp_alarm)
						temp_data.prepare_alarm=temp_prepare_alarm;
					else
						temp_data.prepare_alarm=temp_data.alarm-6;
				}
				else
				{
					temp_data.prepare_alarm=temp_data.alarm-6;
				}		
			}
		}
		printf("change: temp alarm:%d\nchange:temp_prepare_alarm:%d\n",temp_data.alarm,temp_prepare_alarm);
//		eeprom_write_buff(TEMP_ALARM_ADDRESS,(uint8_t*)&temp_data.alarm,TEMP_ALARM_LEN);
//		eeprom_write_buff(TEMP_PREPARE_ALARM_ADDRESS,(uint8_t*)&temp_data.prepare_alarm,TEMP_PREPARE_ALARM_LEN);
		#ifdef EVENT_MODLE
		bsp_event_renew(EVENT_ALARM_CHANGE|EVENT_PREPARE_ALARM_CHANGE);//事件记录
		#endif
}



/*
功能：根据升温速率，进行算法参数配置。
输入：null
输出：null
说明：

*/
void bsp_Temp_Modle_Confige(void)
{
	switch(temp_speed_modle)
	{
		case TEMP_SPEED_MODLE_1:	increment_1=(TEMP_1_ALARM_SOURCE_MIN-TEMP_START)/290;
									increment_2=(TEMP_1_ALARM_SOURCE_MAX-TEMP_1_ALARM_SOURCE_MIN)/160;
									increment_3=(TEMP_END-TEMP_1_ALARM_SOURCE_MAX)/50;
									temp_alarm_source_min=TEMP_1_ALARM_SOURCE_MIN+work.offset;
									temp_alarm_source_max=TEMP_1_ALARM_SOURCE_MAX+work.offset;
									break;
		
		case TEMP_SPEED_MODLE_2:	increment_1=(TEMP_2_ALARM_SOURCE_MIN-TEMP_START)/290;
									increment_2=(TEMP_2_ALARM_SOURCE_MAX-TEMP_2_ALARM_SOURCE_MIN)/160;
									increment_3=(TEMP_END-TEMP_2_ALARM_SOURCE_MAX)/50;
									temp_alarm_source_min=TEMP_2_ALARM_SOURCE_MIN+work.offset;
									temp_alarm_source_max=TEMP_2_ALARM_SOURCE_MAX+work.offset;
									break;
		
		case TEMP_SPEED_MODLE_3:	increment_1=(TEMP_3_ALARM_SOURCE_MIN-TEMP_START)/290;
									increment_2=(TEMP_3_ALARM_SOURCE_MAX-TEMP_3_ALARM_SOURCE_MIN)/160;
									increment_3=(TEMP_END-TEMP_3_ALARM_SOURCE_MAX)/50;
									temp_alarm_source_min=TEMP_3_ALARM_SOURCE_MIN+work.offset;
									temp_alarm_source_max=TEMP_3_ALARM_SOURCE_MAX+work.offset;
									break;
		case TEMP_SPEED_MODLE_4:	
									increment_1=(TEMP_4_ALARM_SOURCE_MIN-TEMP_START)/290;
									increment_2=(TEMP_4_ALARM_SOURCE_MAX-TEMP_4_ALARM_SOURCE_MIN)/160;
									increment_3=(TEMP_END-TEMP_4_ALARM_SOURCE_MAX)/50;
									temp_alarm_source_min=TEMP_4_ALARM_SOURCE_MIN+work.offset;
									temp_alarm_source_max=TEMP_4_ALARM_SOURCE_MAX+work.offset;
									break;
		
		case TEMP_SPEED_MODLE_5:    
									increment_1=(TEMP_5_ALARM_SOURCE_MIN-TEMP_START)/290;
									increment_2=(TEMP_5_ALARM_SOURCE_MAX-TEMP_5_ALARM_SOURCE_MIN)/160;
									increment_3=(TEMP_END-TEMP_5_ALARM_SOURCE_MAX)/50;
									temp_alarm_source_min=TEMP_5_ALARM_SOURCE_MIN+work.offset;
									temp_alarm_source_max=TEMP_5_ALARM_SOURCE_MAX+work.offset;
									break;
		
		case TEMP_SPEED_MODLE_6: 	
									increment_1=(TEMP_6_ALARM_SOURCE_MIN-TEMP_START)/290;
									increment_2=(TEMP_6_ALARM_SOURCE_MAX-TEMP_6_ALARM_SOURCE_MIN)/160;
									increment_3=(TEMP_END-TEMP_6_ALARM_SOURCE_MAX)/50;
									temp_alarm_source_min=TEMP_6_ALARM_SOURCE_MIN+work.offset;
									temp_alarm_source_max=TEMP_6_ALARM_SOURCE_MAX+work.offset;
									break;
		#ifdef TEMP_R							
		/*R型低温速率*/			
		case TEMP_LOW_SPEED_MODLE_0:increment_1=0;
									increment_2=0;
									increment_3=0;
									break;

		case TEMP_LOW_SPEED_MODLE_1:
									increment_1=(TEMP_LOW_1_ALARM_SOURCE_MIN-TEMP_LOW_START)/290;
									increment_2=(TEMP_LOW_1_ALARM_SOURCE_MAX-TEMP_LOW_1_ALARM_SOURCE_MIN)/160
									increment_3=(TEMP_LOW_END-TEMP_LOW_1_ALARM_SOURCE_MAX)/50;
									break;
	
		case TEMP_LOW_SPEED_MODLE_2:
									increment_1=(TEMP_LOW_2_ALARM_SOURCE_MIN-TEMP_LOW_START)/290;
									increment_2=(TEMP_LOW_2_ALARM_SOURCE_MAX-TEMP_LOW_2_ALARM_SOURCE_MIN)/160
									increment_3=(TEMP_LOW_END-TEMP_LOW_2_ALARM_SOURCE_MAX)/50;
									break;
			
		case TEMP_LOW_SPEED_MODLE_3:
									increment_1=(TEMP_LOW_3_ALARM_SOURCE_MIN-TEMP_LOW_START)/290;
									increment_2=(TEMP_LOW_3_ALARM_SOURCE_MAX-TEMP_LOW_3_ALARM_SOURCE_MIN)/160
									increment_3=(TEMP_LOW_END-TEMP_LOW_3_ALARM_SOURCE_MAX)/50;
									break;
		#endif	
		
		default:increment_1=0;
				increment_2=0;
				increment_3=0;
				break;
	}
	printf("increment 1:%.7f\n",increment_1);
	printf("increment 2:%.7f\n",increment_2);
	printf("increment 3:%.7f\n",increment_3);
}	



/*
功能：计算式查表法
输入：null
输出：null
说明：对现有温度实际值进行比例换算成温度有效值
*/

void bsp_Temp_Recension(void)
{
	unsigned short int stage_number=0;
	
	if(temp_flag.speed_cheek)
	{
		if(increment_1==0&&increment_2==0&&increment_3==0)
		{
			temp_data.current=temp_data.source;
		}
		else
		{
			if(temp_alarm_source_max<=temp_data.source&&temp_data.source<(TEMP_END+work.offset))
			{
				stage_number=(temp_data.source-temp_alarm_source_max)/increment_3;
				temp_data.current=TEMP_ALARM_MAX+work.offset+stage_number*0.1;	
				printf("Increment 3:%.7f\n",increment_3);
			}
			else if(temp_alarm_source_min<=temp_data.source&&temp_data.source<temp_alarm_source_max)
			{
				stage_number=(temp_data.source-temp_alarm_source_min)/increment_2;
				temp_data.current=TEMP_ALARM_MIN+work.offset+stage_number*0.1;
				printf("Increment 2:%.7f\n",increment_2);
			}
			else if((TEMP_START+work.offset)<=temp_data.source&&temp_data.source<temp_alarm_source_min)
			{
				stage_number=(temp_data.source-(TEMP_START+work.offset))/increment_1;
				temp_data.current=TEMP_START+work.offset+stage_number*0.1;
				printf("Increment 1:%.7f\n",increment_1);
			}
			else
			{
				temp_data.current=temp_data.source;
			}
			printf("S_NUB:%d\n",stage_number);
		}
	}
	else
	{
		printf("In preparation\n");
		temp_data.current=temp_data.source;
	}	
}	


/*
功能：升温速率检测
输入：null
输出：null1
说明：对温度变化进行速率计算
*/
void bsp_Temp_Speed_Cheek(void)
{	
	static	float temp_first=0;
	static	float temp_second=0;
	static	float temp_speed_d=0;	
	
  if(temp_flag.speed_cheek)
  {
		 printf("SC:ON\n");
		/*当前温度*/
		temp_second=temp_data.source;
		
		if(temp_second !=0&&temp_first !=0)
		{
			temp_speed_d=temp_speed_d+temp_second-temp_first;
			count.speed++;
			printf("D-:%.2f\nS_C:%d\n",temp_speed_d,count.speed);
		}
		/*低温检测*/
		if(work.s)
		{
			bsp_Temp_Low_Cheek();
			printf("LOW Cheek ON\n");
		}
			

		/*保存当次温度供下次运算使用*/
		temp_first=temp_data.source;
		/*速率更新*/
		if(count.speed==25)
		{
			float temp_speed_data=0;
			temp_speed_data=temp_speed_d*60/25;
			temp_data.speed_rate=temp_speed_data;
			/*低温模式的升温速率判断*/
			if(temp_flag.low_start)
			{
				#ifdef R_MODLE
				/*R类检测*/
				if(work.r)
				{
					if(temp_speed_data>=TEMP_6_MODLE_RANGE_E)
					{
						temp_speed_modle=TEMP_LOW_SPEED_MODLE_0;
					}
					/*低温模式3*/
					else if(TEMP_6_MODLE_RANGE_S<=temp_speed_data&&temp_speed_data<TEMP_6_MODLE_RANGE_E)
					{
						temp_speed_modle=TEMP_LOW_SPEED_MODLE_3;
						
					}
					else if(TEMP_5_6_MODLE_RANGE_S<=temp_speed_data&&temp_speed_data<TEMP_5_6_MODLE_RANGE_E)
					{
						if(temp_speed_modle==TEMP_LOW_SPEED_MODLE_3)
							temp_speed_modle=TEMP_LOW_SPEED_MODLE_3;
						else
							temp_speed_modle=TEMP_LOW_SPEED_MODLE_2;
					}
					/*低温模式2*/
					else if(TEMP_5_MODLE_RANGE_S<=temp_speed_data&&temp_speed_data<TEMP_5_MODLE_RANGE_E)
					{
						temp_speed_modle=TEMP_LOW_SPEED_MODLE_2;
					}
					
					else if(TEMP_4_5_MODLE_RANGE_S<=temp_speed_data&&temp_speed_data<TEMP_4_5_MODLE_RANGE_E)
					{
						if(temp_speed_modle==TEMP_LOW_SPEED_MODLE_2)
							temp_speed_modle=TEMP_LOW_SPEED_MODLE_2;
						else
							temp_speed_modle=TEMP_LOW_SPEED_MODLE_1;
					}
					/*低温模式1*/
					else if(TEMP_4_MODLE_RANGE_S<=temp_speed_data&&temp_speed_data<TEMP_4_MODLE_RANGE_E)
					{
						temp_speed_modle=TEMP_LOW_SPEED_MODLE_1;
					}
					else if(0<=temp_speed_data&&temp_speed_data<TEMP_3_4_MODLE_RANGE_E)
					{
						if(temp_speed_modle==TEMP_LOW_SPEED_MODLE_1)
							temp_speed_modle=TEMP_LOW_SPEED_MODLE_1;
						else
							temp_speed_modle=TEMP_LOW_SPEED_MODLE_0;
					}
					else
					{
						printf("temp down!\n");	
					}
				}
				//S模式
				else
				#endif
					temp_speed_modle=TEMP_SPEED_MODLE_N;
			}
			/*z正常模式的速率判断*/
			else
			{
				if(temp_speed_data>=TEMP_6_MODLE_RANGE_E)
				{
					if(temp_speed_modle!=TEMP_SPEED_MODLE_6)
						temp_speed_modle=TEMP_SPEED_MODLE_N;
				}
				/*************************************************模式6:30℃*******************************************************/
				else if(TEMP_6_MODLE_RANGE_S<=temp_speed_data&&temp_speed_data<TEMP_6_MODLE_RANGE_E)
				{
					if(temp_speed_modle>=TEMP_SPEED_MODLE_5&&temp_speed_modle<=TEMP_SPEED_MODLE_6)
						temp_speed_modle=TEMP_SPEED_MODLE_6;//modle_5->modle_6 modle_6->modle_6
					else if(temp_speed_modle<TEMP_SPEED_MODLE_5)
						temp_speed_modle +=2;//modle_4->modle_6 modle_3->modle_5   modle_2->modle_4   modle1->modle_3
				}
				else if(TEMP_5_6_MODLE_RANGE_S<=temp_speed_data&&temp_speed_data<TEMP_5_6_MODLE_RANGE_E)
				{
					if(temp_speed_modle>=TEMP_SPEED_MODLE_5&&temp_speed_modle<TEMP_SPEED_MODLE_6)
						temp_speed_modle=TEMP_SPEED_MODLE_5;//modle_5->modle_5   modle_6->modle_6
					else if(temp_speed_modle<TEMP_SPEED_MODLE_5)
						temp_speed_modle +=2;//modle_4->modle_6  modle_3->modle_5   modle_2->modle_4   modle1->modle_3		
				}
				/*************************************************模式5:20℃*****************************************************/
				else if(TEMP_5_MODLE_RANGE_S<=temp_speed_data&&temp_speed_data<TEMP_5_MODLE_RANGE_E)
				{
					if(temp_speed_modle>=TEMP_SPEED_MODLE_4&&temp_speed_modle<=TEMP_SPEED_MODLE_6)
						temp_speed_modle=TEMP_SPEED_MODLE_5;//modle_4->modle_5  modle_5->modle_5  modle_6->modle_5
					else if(temp_speed_modle<TEMP_SPEED_MODLE_4)
						temp_speed_modle +=2;//modle_3->modle_5 modle_2->modle_4  modle_1->modle_3
				}
				/*******************************************此处开始，允许阶跃二等级**********************************************/
				/*过渡：20℃~10℃*/
				else if(TEMP_4_5_MODLE_RANGE_S<=temp_speed_data&&temp_speed_data<TEMP_4_5_MODLE_RANGE_E)
				{
					if(temp_speed_modle>=TEMP_SPEED_MODLE_5&&temp_speed_modle<=TEMP_SPEED_MODLE_6)
						temp_speed_modle=TEMP_SPEED_MODLE_5;
					else if(temp_speed_modle==TEMP_SPEED_MODLE_4)
						temp_speed_modle=TEMP_SPEED_MODLE_4;
					else if(temp_speed_modle<TEMP_SPEED_MODLE_4)
						temp_speed_modle +=1;//modle_3->modle_4 modle_2->modle_3  modle_1->modle_2
				}
				/**********************************************模式4：10℃*********************************************************/
				else if(TEMP_4_MODLE_RANGE_S<=temp_speed_data&&temp_speed_data<TEMP_4_MODLE_RANGE_E)
				{
					if(temp_speed_modle>=TEMP_SPEED_MODLE_3&&temp_speed_modle<=TEMP_SPEED_MODLE_6)
						temp_speed_modle=TEMP_SPEED_MODLE_4;
					else if(temp_speed_modle<TEMP_SPEED_MODLE_3)
						temp_speed_modle +=1;	
				}
				/*过渡：10℃~5℃*/
				else if(TEMP_3_4_MODLE_RANGE_S<=temp_speed_data&&temp_speed_data<TEMP_3_4_MODLE_RANGE_E)
				{
					if(temp_speed_modle<TEMP_SPEED_MODLE_3)
						temp_speed_modle +=1;
					else
					{
						if(temp_speed_modle==TEMP_SPEED_MODLE_4)
							temp_speed_modle=TEMP_SPEED_MODLE_4;
						else
							temp_speed_modle=TEMP_SPEED_MODLE_3;
					}
				}
				/***********************************************模式3：5℃*******************************************************/
				else if(TEMP_3_MODLE_RANGE_S<=temp_speed_data&&temp_speed_data<TEMP_3_MODLE_RANGE_E)
				{
					if(temp_speed_modle<TEMP_SPEED_MODLE_2)
						temp_speed_modle +=1;
					else
						temp_speed_modle=TEMP_SPEED_MODLE_3;
				}
				/*过渡：5℃~3℃*/
				else if(TEMP_2_3_MODLE_RANGE_S<=temp_speed_data&&temp_speed_data<TEMP_2_3_MODLE_RANGE_E)
				{
					if(temp_speed_modle<TEMP_SPEED_MODLE_2)
						temp_speed_modle +=1;
					else
					{
						if(temp_speed_modle==TEMP_SPEED_MODLE_3)
							temp_speed_modle=TEMP_SPEED_MODLE_3;
						else
							temp_speed_modle=TEMP_SPEED_MODLE_2;
					}
				}
				/*************************************************模式2：3℃******************************************************/
				else if(TEMP_2_MODLE_RANGE_S<=temp_speed_data&&temp_speed_data<TEMP_2_MODLE_RANGE_E)
				{
					temp_speed_modle=TEMP_SPEED_MODLE_2;
				}
				/*过渡：3℃~1℃*/
				else if(TEMP_1_2_MODLE_RANGE_S<=temp_speed_data&&temp_speed_data<TEMP_1_2_MODLE_RANGE_E)
				{
					if(temp_speed_modle==TEMP_SPEED_MODLE_2)
						temp_speed_modle=TEMP_SPEED_MODLE_2;
					else
						temp_speed_modle=TEMP_SPEED_MODLE_1;
				}
				/*************************************************模式1：1℃*******************************************************/
				else if(TEMP_1_MODLE_RANGE_S<=temp_speed_data&&temp_speed_data<TEMP_1_MODLE_RANGE_E)
				{
					temp_speed_modle=TEMP_SPEED_MODLE_1;
				}
				else
				{
					printf("temp down!\n");	
				}
			}
			count.speed=0;	
			temp_speed_d=0;
			temp_second=0;
			temp_first=0;
			
			printf("\nrenew speed rate!\n\n");
			bsp_Temp_Modle_Confige();
		}
  }
  else
  {
		count.speed=0;	
		printf("speed cheek:OFF\n");
  }
	  
}



/*
功能：计算硬件运放放大倍数
输入：temp_srroundings：设备当前环境温度，即设备的NTC传感器的温度值
	  r_ad：当前环境下PT100经过运放后的AD值
输出：运放放大倍数
			
说明：使用时，确保PT100跟NTC处于同一稳定的环境下。

*/
float bsp_PT100_Calibration(float temp_srroundings,uint16_t r_ad)
{
		float a=0;
		float b=0;
		float rt=0;
		float ui=0;
		float p=0;
		uint8_t n=0;
	
		a=fmodf((temp_srroundings),1);//取小数点
		printf("%.2f\n",a);
		b=(uint16_t)temp_srroundings;//取倍数
		n=b+49;//取倍率  表格偏移21个到0度
		rt=PT100_TEMP_TABLE[n];
		rt=rt+(a*(PT100_TEMP_TABLE[n+1]-PT100_TEMP_TABLE[n]));	
		ui=CONSTANT_FLOW_SOURCE*rt;
		p = (r_ad*3.3f/4095)/ui;
//		printf("temp_srroundings rt:					%.2f\n",rt);
//		printf("temp_srroundings ui:					%.7f\n",ui);
		printf("Mf_s:%.7f\n",p);
	
	return p;
}

/*
功能：PT100硬件放大倍数25°校准
输入：NULL
输出：1：校准成功。
	  0：校准失败。
说明：
*/
uint8_t bsp_PT100_Calibration_Temp_25(float surrounding_temp)
{
	uint8_t learn_flag=0;
	float temp_m=0;
	uint16_t temp_s_ad[15]={0};
	uint8_t n=0;
	float temp=0;
	float temp_sr=surrounding_temp;//25.6;
	uint16_t temp_s_ad_v=0;
	
	printf("In the Hardware Calibration Model\n");
	
	for(n=0;n<5;n++)
	{
		delay1ms(200);
		alarm_led_on();
		delay1ms(200);
		alarm_led_off();
		delay1ms(200);
		fault_led_on();
		delay1ms(200);
		fault_led_off();
	}
	n=0;
	for(n=0;n<15;n++)
	{
		float temp_pt100=0;
		bsp_PT100_Temp_Get(&temp_s_ad[n],&temp_pt100,temp_magnification);
		 bsp_deep_sleep();
		printf("%d: PT100 AD:%d\n",n+1,temp_s_ad[n]);
	}
	
	uint8_t b=0;
	uint8_t a=0;
	/*PT100 AD排序*/
	for(b=0;b<15-1;b++)
	{
		for(a=0;a<15-1-b;a++)
		{
			float change=0;
			if(temp_s_ad[a+1]<temp_s_ad[a])
			{
				change=temp_s_ad[a+1];
				temp_s_ad[a+1]=temp_s_ad[a];
				temp_s_ad[a]=change;
			}
		}
	}

	/*求均值*/
	
	temp_s_ad_v=(temp_s_ad[7]+temp_s_ad[8]+temp_s_ad[9]+temp_s_ad[6]+temp_s_ad[5])*0.2+1;
	#ifdef LCCS_MODLE
	if(temp_s_ad_v<1000||temp_s_ad_v>1600)
	#else
	if(temp_s_ad_v<2000||temp_s_ad_v>2600)
	#endif
	{
		temp_data.source=0;
		
		printf("ERROR,Over the Hardware Calibration Model\n");
		
		for(n=0;n<5;n++)
		{
			fault_led_on();
			delay1ms(1000);
			fault_led_off();
			delay1ms(1000);
		}
		return 0;
		
	}
		

	printf("Calibration:temp_s_ad_v:%d\n",temp_s_ad_v);

	temp_m=bsp_PT100_Calibration(surrounding_temp,temp_s_ad_v);//获取硬件放大倍数
	
	printf("Calibration:%.7f\n",temp_m);
	
	float data_save=0;
	
	for(n=0;n<30;n++)
	{
		uint16_t temp_sr_ad=0;
		bsp_PT100_Temp_Get(&temp_sr_ad,&data_save,temp_m);
		 bsp_deep_sleep();
		printf("%d:temp_pt100:%.2f\n",n+1,data_save);
	}
	float temp_source_v=0;
	temp_source_v=data_save;
	
	printf("temp_v:%0.2f\n",temp_source_v);
	
	temp=surrounding_temp-temp_source_v;
	
	printf("(-1,1),D-value:%.2f\n",temp);
	 
	if(temp<=1.0&&temp>=-1.0)
	{
		temp_data.source=0;
		temp_magnification=temp_m;
		eeprom_write_buff(TEMP_ALGORITHM_CALIBRATION_ADDRESS,(uint8_t*)&temp_m,TEMP_ALGORITHM_CALIBRATION_LEN);
		float e_temp_m=0;
		eeprom_read_buff(TEMP_ALGORITHM_CALIBRATION_ADDRESS,(uint8_t*)&e_temp_m,TEMP_ALGORITHM_CALIBRATION_LEN);
		
		printf("OK,Over:%.7f\n",e_temp_m);
		
		for(n=0;n<5;n++)
		{
			alarm_led_on();
			delay1ms(1000);
			alarm_led_off();
			delay1ms(1000);
			
		}
//		uint8_t temp_cheek=8;
//		eeprom_write_buff(TEMP_CALIBRETION_MODLE_ADDRESS,&temp_cheek,TEMP_CALIBRETION_MODLE_LEN);
		return 1;
	}
	else
	{
		temp_data.source=0;
		
		printf("ERROR,Over the Hardware Calibration Model\n");
		
		for(n=0;n<5;n++)
		{
			fault_led_on();
			delay1ms(1000);
			fault_led_off();
			delay1ms(1000);
		}
//					uint8_t temp_cheek=0;
//					eeprom_write_buff(TEMP_CALIBRETION_MODLE_ADDRESS,&temp_cheek,TEMP_CALIBRETION_MODLE_LEN);  
		return 0;
	}
	return 0;
}

/*
低温检测函数
*/
void bsp_Temp_Low_Cheek(void)
{
	/*低温检测*/
	uint8_t temp_low_max=10+work.offset;
	printf("L_reset_count:%d\n",temp_low_reset_count);
	/*8分钟一次检测，低温恢复，一分钟内，若温度大于解除低温模式的温度次数小于10次，则保持低温模式*/
	/*低温恢复*/
	if(temp_flag.low_reset_start)
	{
		if(low_temp.rset_count<60)
		{
			low_temp.rset_count++;
		}
		else
		{
			low_temp.rset_count=0;
			if(low_temp.coincidence_count<10)
			{
				low_temp.coincidence_count=0;	
				temp_low_reset_count=0;
				temp_flag.low_reset_start=0;
			}	
		}
		if(temp_data.source>temp_low_max)
		{
			if(low_temp.coincidence_count<10)
			{
				low_temp.coincidence_count++;
			}
			if(low_temp.coincidence_count==10)
			{
				if(temp_data.speed_rate<2.0)
				{
					temp_speed_modle=TEMP_SPEED_MODLE_1;
					bsp_Temp_Modle_Confige();
					low_temp_count=0;
					temp_flag.low_start=0;//低温模式启动位
					temp_flag.low_reset_start=0;
					low_temp.rset_count=0;
					low_temp.coincidence_count=0;
				}
				else
				{
					//不动作
					low_temp_count=0;//低温达成次数
					temp_flag.low_reset_start=0;//低温恢复检测启动位
					low_temp.rset_count=0;//一分钟计时
					low_temp.coincidence_count=0;//恢复检测一分钟内达条件次数
					temp_low_reset_count=0;//低温检测恢复周期计数清零
				}
			}
		}
	}
	/*低温升温模式进入判断,1分钟内若满足低温模式的次数少于一半，则不进入低温模式*/
	if(!temp_flag.low_start)
	{
		static uint8_t temp_low_cheek_count=0;
		
		if(temp_low_cheek_count<60)
		{
			temp_low_cheek_count++;
		}
		else
		{
			temp_low_cheek_count=0;
			if(low_temp_count <30)
			{
				low_temp_count=0;
			}
		}
		
		if(temp_data.source<=temp_low_max)
		{
			if(low_temp_count<30)
			{
				low_temp_count++;
			}
			if(low_temp_count==30)
			{
				printf("In the low temp modle\n");
				temp_speed_modle=TEMP_SPEED_MODLE_0;
				bsp_Temp_Modle_Confige();
				temp_flag.low_start=1;
				count.speed=0;
				temp_low_cheek_count=0;
			}
		}
	}
}

/*
功能：工作模式切换
输入：work_modle:切换的目标模式
	  data：用于标定的参数
输出：NULL  
说明：
*/

uint8_t bsp_Work_Modle_Change(uint8_t work_modle,uint16_t data0,uint16_t data1,uint8_t init_flag)
{
	float data=0;
	uint8_t e_temp_modle=0;
	uint8_t modle=0;
	modle=work_modle;
	printf("MODLE DATA:%x\n",modle);
	if(init_flag)
	{
		uint8_t e_modle=0;
		eeprom_read_buff(TEMP_MODLE_ADDRESS,(uint8_t*)&e_modle,TEMP_MODLE_LEN);
		printf("READ MODLE DATA:%x\n",e_modle);
		modle=e_modle;
	}
	switch(modle)
	{
		/***********************************A1类*******************************************/
//		case 0:					printf("Get the model zero,that is doesn't exist\n");return 0;
		case TEMP_MODLE_A1:			work.offset=0;e_temp_modle=TEMP_MODLE_A1; work.r=OFF;work.s=OFF;work.bk2=OFF;break;
		case TEMP_MODLE_A1S:    work.offset=0;e_temp_modle=TEMP_MODLE_A1S;work.r=OFF;work.s=ON; work.bk2=OFF;break;
		case TEMP_MODLE_A1R:    work.offset=0;e_temp_modle=TEMP_MODLE_A1R;work.r=ON; work.s=OFF;work.bk2=OFF;break;
		/***********************************A2类*******************************************/
		case TEMP_MODLE_A2:     work.offset=0;e_temp_modle=TEMP_MODLE_A2; work.r=OFF;work.s=OFF;work.bk2=OFF;break;
		case TEMP_MODLE_A2S:    work.offset=0;e_temp_modle=TEMP_MODLE_A2S;work.r=OFF;work.s=ON; work.bk2=OFF;break;
		case TEMP_MODLE_A2R:    work.offset=0;e_temp_modle=TEMP_MODLE_A2R;work.r=ON; work.s=OFF;work.bk2=OFF;break;
		/***********************************B类*******************************************/
		case TEMP_MODLE_B:      work.offset=15;e_temp_modle=TEMP_MODLE_B; work.r=OFF;work.s=OFF;work.bk2=OFF;break;
		case TEMP_MODLE_BS:     work.offset=15;e_temp_modle=TEMP_MODLE_BS;work.r=OFF;work.s=ON; work.bk2=OFF;break;
		case TEMP_MODLE_BR:     work.offset=15;e_temp_modle=TEMP_MODLE_BR;work.r=ON; work.s=OFF;work.bk2=OFF;break;
		/***********************************C类*******************************************/
		case TEMP_MODLE_C:      work.offset=30;e_temp_modle=TEMP_MODLE_C; work.r=OFF;work.s=OFF;work.bk2=OFF;break;
		case TEMP_MODLE_CS:     work.offset=30;e_temp_modle=TEMP_MODLE_CS;work.r=OFF;work.s=ON; work.bk2=OFF;break;
		case TEMP_MODLE_CR:     work.offset=30;e_temp_modle=TEMP_MODLE_CR;work.r=ON; work.s=OFF;work.bk2=OFF;break;
		/***********************************D类*******************************************/
		case TEMP_MODLE_D:      work.offset=45;e_temp_modle=TEMP_MODLE_D; work.r=OFF;work.s=OFF;work.bk2=OFF;break;
		case TEMP_MODLE_DS:     work.offset=45;e_temp_modle=TEMP_MODLE_DS;work.r=OFF;work.s=ON; work.bk2=OFF;break;
		case TEMP_MODLE_DR: 		work.offset=45;e_temp_modle=TEMP_MODLE_DR;work.r=ON; work.s=OFF;work.bk2=OFF;break;
		/***********************************E类*******************************************/
		case TEMP_MODLE_E: 			work.offset=60;e_temp_modle=TEMP_MODLE_E; work.r=OFF;work.s=OFF;work.bk2=OFF;break;
		case TEMP_MODLE_ES: 		work.offset=60;e_temp_modle=TEMP_MODLE_ES;work.r=OFF;work.s=ON; work.bk2=OFF;break;
		case TEMP_MODLE_ER:			work.offset=60;e_temp_modle=TEMP_MODLE_ER;work.r=ON; work.s=OFF;work.bk2=OFF;break;
		/***********************************F类*******************************************/
		case TEMP_MODLE_F: 			work.offset=75;e_temp_modle=TEMP_MODLE_F; work.r=OFF;work.s=OFF;work.bk2=OFF;break;
		case TEMP_MODLE_FS: 		work.offset=75;e_temp_modle=TEMP_MODLE_FS;work.r=OFF;work.s=ON; work.bk2=OFF;break;
		case TEMP_MODLE_FR: 		work.offset=75;e_temp_modle=TEMP_MODLE_FR;work.r=ON; work.s=OFF;work.bk2=OFF;break;
		/***********************************G类*******************************************/
		case TEMP_MODLE_G: 			work.offset=90;e_temp_modle=TEMP_MODLE_G; work.r=OFF;work.s=OFF;work.bk2=OFF;break;
		case TEMP_MODLE_GS:	    work.offset=90;e_temp_modle=TEMP_MODLE_GS;work.r=OFF;work.s=ON; work.bk2=OFF;break;
		case TEMP_MODLE_GR: 		work.offset=90;e_temp_modle=TEMP_MODLE_GR;work.r=ON; work.s=OFF;work.bk2=OFF;break;
		#ifdef LCCS_MODLE	
				/********************************博康模式1类模式*******************************************/
		case BK_A1:work.offset=105;e_temp_modle=BK_A1;work.r=OFF;work.s=OFF;work.bk2=OFF;break;
		case BK_B1:work.offset=120;e_temp_modle=BK_B1;work.r=OFF;work.s=OFF;work.bk2=OFF;break;
		case BK_C1:work.offset=135;e_temp_modle=BK_C1;work.r=OFF;work.s=OFF;work.bk2=OFF;break;
		case BK_D1:work.offset=150;e_temp_modle=BK_D1;work.r=OFF;work.s=OFF;work.bk2=OFF;break;
		case BK_E1:work.offset=165;e_temp_modle=BK_E1;work.r=OFF;work.s=OFF;work.bk2=OFF;break;
		case BK_F1:work.offset=180;e_temp_modle=BK_F1;work.r=OFF;work.s=OFF;work.bk2=OFF;break;
		case BK_G1:work.offset=195;e_temp_modle=BK_G1;work.r=OFF;work.s=OFF;work.bk2=OFF;break;
		case BK_H1:work.offset=210;e_temp_modle=BK_H1;work.r=OFF;work.s=OFF;work.bk2=OFF;break;
		case BK_I1:work.offset=225;e_temp_modle=BK_I1;work.r=OFF;work.s=OFF;work.bk2=OFF;break;
		case BK_J1:work.offset=240;e_temp_modle=BK_J1;work.r=OFF;work.s=OFF;work.bk2=OFF;break;
		case BK_K1:work.offset=255;e_temp_modle=BK_K1;work.r=OFF;work.s=OFF;work.bk2=OFF;break;
		case BK_L1:work.offset=270;e_temp_modle=BK_L1;work.r=OFF;work.s=OFF;work.bk2=OFF;break;	
		case BK_M1:work.offset=285;e_temp_modle=BK_M1;work.r=OFF;work.s=OFF;work.bk2=OFF;break;
		case BK_N1:work.offset=300;e_temp_modle=BK_N1;work.r=OFF;work.s=OFF;work.bk2=OFF;break;
		case BK_O1:work.offset=315;e_temp_modle=BK_O1;work.r=OFF;work.s=OFF;work.bk2=OFF;break;
		case BK_P1:work.offset=330;e_temp_modle=BK_P1;work.r=OFF;work.s=OFF;work.bk2=OFF;break;
		case BK_Q1:work.offset=345;e_temp_modle=BK_Q1;work.r=OFF;work.s=OFF;work.bk2=OFF;break;
		case BK_R1:work.offset=360;e_temp_modle=BK_R1;work.r=OFF;work.s=OFF;work.bk2=OFF;break;
		case BK_S1:work.offset=375;e_temp_modle=BK_S1;work.r=OFF;work.s=OFF;work.bk2=OFF;break;
		#endif
		/***********************************校准模式*******************************************/
		case TEMP_CALIBRATION_MODLE:	
									if(init_flag)
									{
										printf("Get the calibration model,but that's not reasonable\n");
										printf("Init the modle\n");
										#ifdef JTW_HD_BK500_427
										/*默认DS类*/
										work.offset=45;e_temp_modle=TEMP_MODLE_DS;work.r=OFF;work.bk2=OFF;	
										bsp_fault_to_alarm_led_flashing(30,1);
										#elif	JTW_HD_BK500
										/*默认A2S类*/
										 work.offset=0;e_temp_modle=TEMP_MODLE_A2S;work.r=OFF;work.ht=OFF;
										bsp_fault_to_alarm_led_flashing(30,1);
										#elif JTW_HD_BK500_L
										/*默认DS类*/
										work.offset=45;e_temp_modle=TEMP_MODLE_DS;work.r=OFF;work.ht=OFF;	
										bsp_fault_to_alarm_led_flashing(30,1);
										#elif JTW_HD_BK500_E
										/*默认DS类*/
										work.offset=45;e_temp_modle=TEMP_MODLE_DS;work.r=OFF;work.ht=OFF;	
										bsp_fault_to_alarm_led_flashing(30,1);
										#endif
										break;
									}
									data=data1*0.01;
									data=data+data0;
									printf("calibration value:%.2f\n",data);
									bsp_PT100_Calibration_Temp_25(data);
									#ifdef EVENT_MODLE
									bsp_event_renew(EVENT_CALIBRATION);
									#endif
									printf("calibreation modle end\n");
									return 0;
		#ifdef LCCS_MODLE					
					
		/********************************博康模式2类模式*******************************************/
		case BK_A2:work.offset=0;e_temp_modle=BK_A2;work.r=OFF;work.s=OFF;work.bk2=ON;break;
		case BK_B2:work.offset=0;e_temp_modle=BK_B2;work.r=OFF;work.s=OFF;work.bk2=ON;break;
		case BK_C2:work.offset=0;e_temp_modle=BK_C2;work.r=OFF;work.s=OFF;work.bk2=ON;break;
		case BK_D2:work.offset=0;e_temp_modle=BK_D2;work.r=OFF;work.s=OFF;work.bk2=ON;break;
		case BK_E2:work.offset=0;e_temp_modle=BK_E2;work.r=OFF;work.s=OFF;work.bk2=ON;break;
		case BK_F2:work.offset=0;e_temp_modle=BK_F2;work.r=OFF;work.s=OFF;work.bk2=ON;break;
		case BK_G2:work.offset=0;e_temp_modle=BK_G2;work.r=OFF;work.s=OFF;work.bk2=ON;break;
		case BK_H2:work.offset=0;e_temp_modle=BK_H2;work.r=OFF;work.s=OFF;work.bk2=ON;break;
		case BK_I2:work.offset=0;e_temp_modle=BK_I2;work.r=OFF;work.s=OFF;work.bk2=ON;break;
		case BK_J2:work.offset=0;e_temp_modle=BK_J2;work.r=OFF;work.s=OFF;work.bk2=ON;break;
		case BK_K2:work.offset=0;e_temp_modle=BK_K2;work.r=OFF;work.s=OFF;work.bk2=ON;break;
		case BK_L2:work.offset=0;e_temp_modle=BK_L2;work.r=OFF;work.s=OFF;work.bk2=ON;break;
		/***********************************错误*******************************************/
		#endif
		default:   
					
					if(init_flag)
					{
						printf("Get the model zero,that doesn't exist\n");
						printf("Init the modle\n");
						#ifdef JTW_HD_BK500_427
						/*默认DS类*/
						work.offset=45;e_temp_modle=TEMP_MODLE_DS;work.r=OFF;work.s=ON;work.bk2=OFF;	
						bsp_fault_to_alarm_led_flashing(30,2);
						#elif	JTW_HD_BK500
						/*默认A2S类*/
						 work.offset=0;e_temp_modle=TEMP_MODLE_A2S;work.r=OFF;work.s=ON;work.bk2=OFF;
						bsp_fault_to_alarm_led_flashing(30,2);
						#elif JTW_HD_BK500_L
						/*默认DS类*/
						work.offset=45;e_temp_modle=TEMP_MODLE_DS;work.r=OFF;work.s=ON;work.bk2=OFF;	
						bsp_fault_to_alarm_led_flashing(30,2);
						#elif JTW_HD_BK500_E
						/*默认DS类*/
						work.offset=45;e_temp_modle=TEMP_MODLE_DS;work.r=OFF;work.s=ON;work.bk2=OFF;	
						bsp_fault_to_alarm_led_flashing(30,2);
						#endif
					}	
					else
					{
//						bsp_alarm_fault_led_flashing(30,5);
						bsp_fault_led_flashing(30,5);
						printf("\nFault,do not renew the modle\n\n");
						return 0;	
					}
					break;
	}
	printf("MODLE RENEW OK\n");
	#ifdef LCCS_MODLE
	if(work.bk2)
	{
		switch(e_temp_modle)
		{
			case BK_A2:bsp_Temp_Alarm_Change(BK_A2_ALARM,BK_A2_PREPARE_ALARM,1);break;
			case BK_B2:bsp_Temp_Alarm_Change(BK_B2_ALARM,BK_B2_PREPARE_ALARM,1);break;
			case BK_C2:bsp_Temp_Alarm_Change(BK_C2_ALARM,BK_C2_PREPARE_ALARM,1);break;
			case BK_D2:bsp_Temp_Alarm_Change(BK_D2_ALARM,BK_D2_PREPARE_ALARM,1);break;
			case BK_E2:bsp_Temp_Alarm_Change(BK_E2_ALARM,BK_E2_PREPARE_ALARM,1);break;
			case BK_F2:bsp_Temp_Alarm_Change(BK_F2_ALARM,BK_F2_PREPARE_ALARM,1);break;
			case BK_G2:bsp_Temp_Alarm_Change(BK_G2_ALARM,BK_G2_PREPARE_ALARM,1);break;
			case BK_H2:bsp_Temp_Alarm_Change(BK_H2_ALARM,BK_H2_PREPARE_ALARM,1);break;
			case BK_I2:bsp_Temp_Alarm_Change(BK_I2_ALARM,BK_I2_PREPARE_ALARM,1);break;
			case BK_J2:bsp_Temp_Alarm_Change(BK_J2_ALARM,BK_J2_PREPARE_ALARM,1);break;
			case BK_K2:bsp_Temp_Alarm_Change(BK_K2_ALARM,BK_K2_PREPARE_ALARM,1);break;
			case BK_L2:bsp_Temp_Alarm_Change(BK_L2_ALARM,BK_L2_PREPARE_ALARM,1);break;
		}
		bsp_alarm_to_fault_led_flashing(30,1);
	}
	else
	{
	#endif
		if(e_temp_modle==TEMP_MODLE_A1S||e_temp_modle==TEMP_MODLE_A1||e_temp_modle==TEMP_MODLE_A1R)
		{
			bsp_Temp_Alarm_Change(TEMP_ALARM+work.offset-4,TEMP_ALARM+work.offset-10,0);
		}
		else
		{
			bsp_Temp_Alarm_Change(TEMP_ALARM+work.offset,TEMP_ALARM+work.offset-10,0);
		}
		bsp_fault_to_alarm_led_flashing(30,1);
	#ifdef LCCS_MODLE
	}
	#endif
	/*模式写入保存*/
	eeprom_write_buff(TEMP_MODLE_ADDRESS,(uint8_t*)&e_temp_modle,TEMP_MODLE_LEN);
	work.modle=e_temp_modle;
	/*低温模式恢复*/
	temp_speed_modle=TEMP_SPEED_MODLE_1;
	bsp_Temp_Modle_Confige();
	if(init_flag)
	{
		return 1;
	}
	low_temp_count=0;//低温达成次数
	temp_flag.low_start=0;//低温模式启动位
	temp_flag.low_reset_start=0;//低温模式恢复启动位
	low_temp.rset_count=0;//一分钟计时
	low_temp.coincidence_count=0;//恢复检测一分钟内达条件次数
	#ifdef EVENT_MODLE	
	/*事件记录*/
	if(work_modle!=TEMP_CALIBRATION_MODLE)
		bsp_event_renew(EVENT_MODLE_CHANGE);//事件记录
	#endif
	return 1;
}

/*
功能：PT100温度采集
输入：temp_souorce_ad:采样的AD值保存
	  temp_source：温度实际值
	  magnification：计算放大倍数
	  
输出：1：采样正常
      0：采样异常
	  
说明：

*/

uint8_t bsp_PT100_Temp_Get(uint16_t *temp_source_ad,float *temp_source,float magnification)
{
	float Ui=0;
	float Uad=0;
	float Rt=0;
	uint16_t r_ad=0;
	
	Sysctrl_SetPeripheralGate(SysctrlPeripheralGpio, TRUE);	//打开GPIO时钟
	Sysctrl_SetPeripheralGate(SysctrlPeripheralAdcBgr,TRUE);//打开ADC，BGR时钟	
		
	PT100_POWER_ON;

	M0P_BGR->CR_f.BGR_EN = 1;//使能BGR
	M0P_ADC->CR0_f.EN=1;//使能ADC

	delay1ms(15);
	/*单采*/
	M0P_ADC->CR1_f.MODE=0;//单次转化模式
	M0P_ADC->CR0_f.REF=3;//选择内部AVCC参考电压
	M0P_ADC->CR0_f.SAM=3;//转换周期12个
	M0P_ADC->CR0_f.CLKDIV=3;//8分频
	M0P_ADC->CR0_f.SGLMUX=TEMP_AD_CH;	
		
	M0P_ADC->ICR_f.SGLIC=0;//清除单次转换标志位	
	M0P_ADC->SGLSTART_f.START=1;//启动转换
	while(!(M0P_ADC->IFR_f.SGLIF)){}//等待转换完成

	r_ad=M0P_ADC->RESULT_f.RESULT;//获取采温AD-TP100
	PT100_POWER_OFF;
		
	M0P_BGR->CR_f.BGR_EN = 0;//关闭BGR
	M0P_ADC->CR0_f.EN=0;//关闭ADC
	Sysctrl_SetPeripheralGate(SysctrlPeripheralAdcBgr,FALSE);//关闭ADC，BGR时钟	

	*temp_source_ad=r_ad;
//	r_ad=3250;//506℃
	printf("Rt_ad: [%d]\n",r_ad);
	/*保留断路检测判断，虽然高温与短路现象一致，但不能误报*/
	#ifdef LCCS_MODLE
	if(r_ad<=800||r_ad>=3900)		
	#else
	if(r_ad<=800||r_ad>=3900)	
	#endif
	{
		Ui=(r_ad*3.3f/4095)/magnification;//TP100 电压
		Rt=Ui*CONSTANT_FLOW_SOURCE;			//TP100 阻值	
		*temp_source=1;
		temp_data.source=1;
		printf("fault Rt :[%.2f]\n\n",Rt);
		return 0;
	}
	Uad=r_ad*3.3f/4095;
	Ui=Uad/magnification;//TP100 电压
	Rt=Ui/CONSTANT_FLOW_SOURCE;
	printf("Uad:   [%.3f]\nUi:    [%.3f]\nRt:    [%.2f]\nMF:    [%.3f]\n",Uad,Ui,Rt,magnification);
	/*temp：-49-509*/
	uint16_t max,min,mid;
	min=0;
	max=(TABLE_COUNT-1);	
	/*查表换算温度值*/
	if(Rt<PT100_TEMP_TABLE[min])
	{
		*temp_source=-40.00;
		printf("So low,Not in scope!\n");
		return 1;
	}
	else if(Rt>PT100_TEMP_TABLE[max])
	{
		#ifdef LCCS_MODLE
		*temp_source=610.00;
		#else
		*temp_source=250.00;
		#endif
		printf("So, hight,Not in scope!\n");
		return 1;
	}
	/*缩短查表时间*/
	if(Rt>=PT100_TEMP_TABLE[TABLE_COUNT*3/4])
		min=TABLE_COUNT*3/4;		
	else if(Rt>=PT100_TEMP_TABLE[TABLE_COUNT/2])
	{
		max=TABLE_COUNT*3/4;
		min=TABLE_COUNT/2;
	}
	else if(Rt>=PT100_TEMP_TABLE[TABLE_COUNT/4])
	{
		max=TABLE_COUNT/2;
		min=TABLE_COUNT/4;
	}
	else
		max=TABLE_COUNT/4;
	
	while(min<max)
	{
		mid=(max+min)>>1;
		if(Rt>=PT100_TEMP_TABLE[mid]&&Rt<PT100_TEMP_TABLE[mid+1])
		{
			//printf("PT1000 :   [mid:%.2f],[mid+1:%.2f], mid:%d \n\n",PT100_TEMP_TABLE[mid],PT100_TEMP_TABLE[mid+1],mid);
			if(mid<49)
			{
				*temp_source=(mid-48)-((PT100_TEMP_TABLE[mid+1]-Rt)/(PT100_TEMP_TABLE[mid+1]-PT100_TEMP_TABLE[mid]));
			}
			else
			{
				*temp_source=(mid-49)+((Rt-PT100_TEMP_TABLE[mid])/(PT100_TEMP_TABLE[mid+1]-PT100_TEMP_TABLE[mid]));
			}
			#define MEDIAN_FILTER OFF
			#if MEDIAN_FILTER
			/*中值滤波s*/
			static float temp_a[7]={0};
			static uint8_t z_count=0;//中值滤波计数
			temp_a[z_count]=*temp_source;
			if(z_count<7)
				z_count++;
			if(z_count==7)
			{
				z_count=0;
				if(!low_temp.a_start)
					low_temp.a_start=1;
			}
			if(!low_temp.a_start)
				return 0;
			
			float temp_a_e[7]={0};
			for(uint8_t s=0;s<7;s++)
			{
				temp_a_e[s]=temp_a[s];
			}
			
			uint8_t b=0;
			uint8_t a=0;
			for(b=0;b<7-1;b++)
			{
				for(a=0;a<7-1-b;a++)
				{
					float change=0;
					if(temp_a_e[a+1]<temp_a_e[a])
					{
						change=temp_a_e[a+1];
						temp_a_e[a+1]=temp_a_e[a];
						temp_a_e[a]=change;
					}
				}
			}
			*temp_source=(temp_a_e[1]+temp_a_e[2]+temp_a_e[3]+temp_a_e[4]+temp_a_e[5])*0.2;
			/*中值滤波E*/
			
			#else
			printf("TS :    [%.2f]\n\n",*temp_source);
			*temp_source=bsp_Filtering_Alogorithm(*temp_source);
			#endif
			return 1;
		}
		if(Rt<PT100_TEMP_TABLE[mid])
			max=mid;
		else
			min=mid;	
	}	
	return 1;
}

float bsp_NTC_Temp_Get(void)
{
	uint16_t sr_ad_v[5]={0};
	uint16_t sr_ad=0;
	float 	 temp=0;
	
	Sysctrl_SetPeripheralGate(SysctrlPeripheralGpio, TRUE);	//打开GPIO时钟
	Sysctrl_SetPeripheralGate(SysctrlPeripheralAdcBgr,TRUE);//打开ADC，BGR时钟	
	
	NTC_POWER_ON;

	M0P_BGR->CR_f.BGR_EN = 1;//使能BGR
	M0P_ADC->CR0_f.EN=1;//使能ADC

	delay1ms(1);
	M0P_ADC->CR1_f.MODE=0;//单次转化模式
	M0P_ADC->CR0_f.REF=3;//选择内部AVCC参考电压
	M0P_ADC->CR0_f.SAM=3;//转换周期12个
	M0P_ADC->CR0_f.CLKDIV=3;//8分频
	M0P_ADC->CR0_f.SGLMUX=TEMP_S_AD_CH;
	
	uint8_t v=0;
	
	M0P_ADC->ICR_f.SGLIC=0;//清除单次转换标志位	

	M0P_ADC->SGLSTART_f.START=1;//启动转换

	while(!(M0P_ADC->IFR_f.SGLIF)){}//等待转换完成

	sr_ad = M0P_ADC->RESULT_f.RESULT;//获取采温AD-TP100			
	
	NTC_POWER_OFF;	
		
	M0P_BGR->CR_f.BGR_EN = 0;//关闭BGR
	M0P_ADC->CR0_f.EN=0;//关闭ADC
	
	temp=Temp_AD_To_Temp_Source(sr_ad);//环境温度

	return temp;
}

/*
Q:过程噪声，Q增大，动态响应变快，收敛稳定性变坏
R:测量噪声，R增大，动态响应变慢，收敛稳定性变好
*/
float bsp_Filtering_Alogorithm(float temp)
{
	
	static float x_last = 0;
	static float p_last = 0;
	float Q = 0.020;
	float R = 0.620;
	float kg;
	float x_mid;
	float x_now;
	float p_mid;
	float p_now;

	x_mid = x_last;	
	p_mid=p_last+Q;	
	kg=p_mid/(p_mid +R);
	x_now = x_mid +kg*(temp - x_mid);
	p_now =(1-kg)*p_mid;

	p_last = p_now;	
	x_last = x_now;
	printf("\np_now: [%.7f]\n\n",p_now);
	return x_now;
}

//#ifdef LCCS_MODLE
///*高温模式补偿 针对油炸机φ8的探头*/
//void bsp_hight_temp_recension(void)
//{
//	if(TEMP_SOURCE_245<=temp_data.source&&temp_data.source<TEMP_SOURCE_255)
//	{
//		temp_data.current=245.00+((temp_data.source-TEMP_SOURCE_245)*10)/(TEMP_SOURCE_255-TEMP_SOURCE_245);
//	}
//	else if(TEMP_SOURCE_235<=temp_data.source)
//	{
//		temp_data.current=235.00+((temp_data.source-TEMP_SOURCE_235)*10)/(TEMP_SOURCE_245-TEMP_SOURCE_235);
//	}
//	else if(TEMP_SOURCE_225<=temp_data.source)
//	{
//		temp_data.current=225.00+((temp_data.source-TEMP_SOURCE_225)*10)/(TEMP_SOURCE_235-TEMP_SOURCE_225);
//	}
//	else if(TEMP_SOURCE_215<=temp_data.source)
//	{
//		temp_data.current=215.00+((temp_data.source-TEMP_SOURCE_215)*10)/(TEMP_SOURCE_225-TEMP_SOURCE_215);
//	}
//	else if(TEMP_SOURCE_205<=temp_data.source)
//	{
//		temp_data.current=205.00+((temp_data.source-TEMP_SOURCE_205)*10)/(TEMP_SOURCE_215-TEMP_SOURCE_205);
//	}
//	else if(TEMP_SOURCE_195<=temp_data.source)
//	{
//		temp_data.current=195.00+((temp_data.source-TEMP_SOURCE_195)*10)/(TEMP_SOURCE_205-TEMP_SOURCE_195);
//	}
//	else if(TEMP_SOURCE_185<=temp_data.source)
//	{
//		temp_data.current=185.00+((temp_data.source-TEMP_SOURCE_185)*10)/(TEMP_SOURCE_195-TEMP_SOURCE_185);
//	}
//	else if(TEMP_SOURCE_175<=temp_data.source)
//	{
//		temp_data.current=175.00+((temp_data.source-TEMP_SOURCE_175)*10)/(TEMP_SOURCE_185-TEMP_SOURCE_175);
//	}
//	else if(TEMP_SOURCE_165<=temp_data.source)
//	{
//		temp_data.current=165.00+((temp_data.source-TEMP_SOURCE_165)*10)/(TEMP_SOURCE_175-TEMP_SOURCE_165);
//	}
//	else if(TEMP_SOURCE_155<=temp_data.source)
//	{
//		temp_data.current=155.00+((temp_data.source-TEMP_SOURCE_155)*10)/(TEMP_SOURCE_165-TEMP_SOURCE_155);
//	}
//	else if(TEMP_SOURCE_145<=temp_data.source)
//	{
//		temp_data.current=145.00+((temp_data.source-TEMP_SOURCE_145)*10)/(TEMP_SOURCE_155-TEMP_SOURCE_145);
//	}
//	else if(TEMP_SOURCE_135<=temp_data.source)
//	{
//		temp_data.current=135.00+((temp_data.source-TEMP_SOURCE_135)*10)/(TEMP_SOURCE_145-TEMP_SOURCE_135);
//	}
//	else if(TEMP_SOURCE_125<=temp_data.source)
//	{
//		temp_data.current=125.00+((temp_data.source-TEMP_SOURCE_125)*10)/(TEMP_SOURCE_135-TEMP_SOURCE_125);
//	}
//	else if(TEMP_SOURCE_115<=temp_data.source)
//	{
//		temp_data.current=115.00+((temp_data.source-TEMP_SOURCE_115)*10)/(TEMP_SOURCE_125-TEMP_SOURCE_115);
//	}
//	else if(TEMP_SOURCE_105<=temp_data.source)
//	{
//		temp_data.current=105.00+((temp_data.source-TEMP_SOURCE_105)*10)/(TEMP_SOURCE_115-TEMP_SOURCE_105);
//	}
//	else if(TEMP_SOURCE_95<=temp_data.source)
//	{
//		temp_data.current=95.00+((temp_data.source-TEMP_SOURCE_95)*10)/(TEMP_SOURCE_105-TEMP_SOURCE_95);
//	}
//	else if(TEMP_SOURCE_85<=temp_data.source)
//	{
//		temp_data.current=85.00+((temp_data.source-TEMP_SOURCE_85)*10)/(TEMP_SOURCE_95-TEMP_SOURCE_85);
//	}
//	else if(TEMP_SOURCE_75<=temp_data.source)
//	{
//		temp_data.current=75.00+((temp_data.source-TEMP_SOURCE_75)*10)/(TEMP_SOURCE_85-TEMP_SOURCE_75);
//	}
//	else if(TEMP_SOURCE_65<=temp_data.source)
//	{
//		temp_data.current=65.00+((temp_data.source-TEMP_SOURCE_65)*10)/(TEMP_SOURCE_75-TEMP_SOURCE_65);
//	}
//	else if(TEMP_SOURCE_55<=temp_data.source)
//	{
//		temp_data.current=55.00+((temp_data.source-TEMP_SOURCE_55)*10)/(TEMP_SOURCE_65-TEMP_SOURCE_55);
//	}
//	else if(TEMP_SOURCE_45<=temp_data.source)
//	{
//		temp_data.current=45.00+((temp_data.source-TEMP_SOURCE_45)*10)/(TEMP_SOURCE_55-TEMP_SOURCE_45);
//	}
//	else if(TEMP_SOURCE_35<=temp_data.source)
//	{
//		temp_data.current=35.00+((temp_data.source-TEMP_SOURCE_35)*10)/(TEMP_SOURCE_45-TEMP_SOURCE_35);
//	}
//	else if(TEMP_SOURCE_25<=temp_data.source)
//	{
//		temp_data.current=25.00+((temp_data.source-TEMP_SOURCE_25)*10)/(TEMP_SOURCE_35-TEMP_SOURCE_25);
//	}
//	else
//	{
//		temp_data.current=temp_data.source;
//	}

//}

////#endif
//void test(void)
//{
//	static float Rt= 80.70;
//	/*temp：-49-509*/
//	while(Rt<=PT100_TEMP_TABLE[TABLE_COUNT-1])
//	{
//	float temp=0;
//	uint16_t max,min,mid;
//	min=0;
//	max=(TABLE_COUNT-1);	
//	/*查表换算温度值*/
//	if(Rt<PT100_TEMP_TABLE[min])
//	{
//		printf("So low,Not in scope!\n");
//		break;
//	}
//	else if(Rt>PT100_TEMP_TABLE[max])
//	{
//		printf("So, hight,Not in scope!\n");
//		break;
//	}
//	if(Rt>=PT100_TEMP_TABLE[TABLE_COUNT*3/4])
//		min=TABLE_COUNT*3/4;		
//	else if(Rt>=PT100_TEMP_TABLE[TABLE_COUNT/2])
//	{
//		max=TABLE_COUNT*3/4;
//		min=TABLE_COUNT/2;
//	}
//	else if(Rt>=PT100_TEMP_TABLE[TABLE_COUNT/4])
//	{
//		max=TABLE_COUNT/2;
//		min=TABLE_COUNT/4;
//	}
//	else
//		max=TABLE_COUNT/4;
//	
//	while(min<max)
//	{
//		mid=(max+min)>>1;
//		if(Rt>=PT100_TEMP_TABLE[mid]&&Rt<PT100_TEMP_TABLE[mid+1])
//		{
//			if(mid<49)
//			{
//				temp=(mid-48)-((PT100_TEMP_TABLE[mid+1]-Rt)/(PT100_TEMP_TABLE[mid+1]-PT100_TEMP_TABLE[mid]));
//			}
//			else
//			{
//				temp=(mid-49)+((Rt-PT100_TEMP_TABLE[mid])/(PT100_TEMP_TABLE[mid+1]-PT100_TEMP_TABLE[mid]));
//			}
//			printf("[%.2f]\n",temp);
//			Rt+=0.05;
//			break;
//		}
//		if(Rt<PT100_TEMP_TABLE[mid])
//			max=mid;
//		else
//			min=mid;	
//	}	
//	
//	}
//	Rt=80.70;
//	printf("over\n");

//}

